import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
// tslint:disable-next-line: max-line-length
import { PageVo, LibraryListVo, LibraryDetailVo, LibCount, ThirdPartyLibVo, AddVo, LicenseIdListVo } from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})

export class LibraryService {
  resData: {};

  constructor(
    private http: HttpClient,
    private apiService: ApiService
  ) { }

  downloadFile(proxyId?: string) {
    return this.apiService.get('/agent/java/123', { responseType: 'blob' });
  }

  /**
   * 获取第三方库的列表和过滤信息
   * @param pageIndex 页面
   * @param pageSize 一页的数据
   * @param filter 过滤条件
   */
  getLibsPage(pageIndex: number = 1, pageSize: number = 10, filter: string): Observable<PageVo<LibraryListVo>> {
    if (filter !== null && filter !== undefined && filter !== '') {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        console.log('Janine===params==2==', params);
        return this.apiService.get('/libs', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<LibraryListVo>>) => jsonResult.data)
          );
      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('Janine===params==3==', params);
      return this.apiService.get('/libs', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<LibraryListVo>>) => jsonResult.data)
        );
    }

  }

  // 数据请求
  getLibraryPage(pageIndex: number = 1, pageSize: number = 10,
    order: string = 'file_name', sort: string = 'desc'): Observable<PageVo<LibraryListVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`)
      .append('order', order)
      .append('sort', sort);
    return this.apiService.get('/libs', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<LibraryListVo>>) => jsonResult.data)
      );
  }
  // 数据请求
  getCVEFilterPage(filter: string, pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<LibraryListVo>> {
    const params = new HttpParams()
      .append('filter', filter)
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/libs', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<LibraryListVo>>) => jsonResult.data)
      );
  }

  // 详情页数据
  getLibraryDetailPage(hash: string): Observable<PageVo<LibraryDetailVo>> {
    return this.apiService.get('/libraries/' + hash, {})
      .pipe(
        map((jsonResult: JsonResult<PageVo<LibraryDetailVo>>) => jsonResult.data)
      );
  }

  getLibraryPageByProjectId(appUuid: string, pageIndex: number = 1, pageSize: number = 10,
    order: string = 'file_name', sort: string = 'desc'): Observable<PageVo<LibraryListVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`)
      .append('order', order)
      .append('sort', sort);
    return this.apiService.get(`/application/libraries/${appUuid}`, { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<LibraryListVo>>) => jsonResult.data)
      );
  }

  getLibraryCount(): Observable<LibCount> {
    return this.apiService.get('/thirdPartyLib/data/count')
      .pipe(
        map((jsonResult: JsonResult<LibCount>) => jsonResult.data)
      );
  }

  getThirdPartyLibList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<ThirdPartyLibVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/thirdPartyLib/data/list', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<ThirdPartyLibVo>>) => jsonResult.data)
      );
  }

  addLibrary(resData: any) {
    console.log('== resData ==', resData);
    return this.apiService.post('/thirdPartyLib/data/add', resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }

  downloadJar(title: string, type: string) {
    if (type === 'json' && title === '组件') {
      return this.apiService.get('/export/jarinfo/json', { responseType: 'blob' });
    }
    if (type === 'xml' && title === '组件') {
      return this.apiService.get('/export/jarinfo/xml', { responseType: 'blob' });
    }

    if (type === 'json' && title === 'License') {
      return this.apiService.get('/export/license/json', { responseType: 'blob' });
    }
    if (type === 'xml' && title === 'License') {
      return this.apiService.get('/export/license/xml', { responseType: 'blob' });
    }
    if (type === 'xml' && title === 'cve') {
      return this.apiService.get('/export/cve/xml', { responseType: 'blob' });
    }
    if (type === 'json' && title === 'cve') {
      return this.apiService.get('/export/cve/json', { responseType: 'blob' });
    }

  }
  //  获取library.modal 中licenseIds下拉框
  getSelectLicenseId() {
    return this.apiService.get('/jarinfo/select/licenseid').pipe(
      map((jsonResult: JsonResult<LicenseIdListVo>) => jsonResult)
    );
  }
}
